export default {
    // 获取今天
    getNow:()=>{
        return new Date().getTime();
    },
    // 日期转毫秒数
    turnM:(date:string)=>{
        date = date.replace(/-/g, "/");
        return new Date(date).getTime();
    },
    // 毫秒数转各种格式
    turn:(time:number|string,type:string)=>{
        time=time.toString();
        if(time.length==10){
            time+='000';
        }
        time = parseInt(time);
        const date = new Date(time);
        let year = date.getFullYear();
        let month = date.getMonth()+1;
        let day = date.getDate();
        let hour = date.getHours();
        let min = date.getMinutes();
        let sen = date.getSeconds();
        let zeroMonth;
        let zeroDay;
        let zeroHour;
        let zeroMin;
        let zeroSen;
        if(month<10){
            zeroMonth = `0${month}`;
        }else{
            zeroMonth = month;
        }
        if(day<10){
            zeroDay = `0${day}`;
        }else{
            zeroDay = day;
        }
        if(hour<10){
            zeroHour = `0${hour}`;
        }else{
            zeroHour = hour;
        }
        if(min<10){
            zeroMin = `0${min}`;
        }else{
            zeroMin = min;
        }
        if(sen<10){
            zeroSen = `0${sen}`;
        }else{
            zeroSen = sen;
        }
        switch(type) {
            case 'YYYY-MM-DD hh:mm:ss':
                return `${year}-${zeroMonth}-${zeroDay} ${zeroHour}:${zeroMin}:${zeroSen}`;
            case 'YYYY-MM-DD hh:mm':
                return `${year}-${zeroMonth}-${zeroDay} ${zeroHour}:${zeroMin}`;
            case 'YYYY-MM-DD hh':
                return `${year}-${zeroMonth}-${zeroDay} ${zeroHour}`;
            case 'YYYY-MM-DD':
                return `${year}-${zeroMonth}-${zeroDay}`;
            case 'YYYY-MM':
                    return `${year}-${zeroMonth}`;
            case 'YYYY-M-D hh:mm:ss':
                return `${year}-${month}-${day} ${zeroHour}:${zeroMin}:${zeroSen}`;
            case 'YYYY-M-D hh:mm':
                return `${year}-${month}-${day} ${zeroHour}:${zeroMin}`;
            case 'YYYY-M-D hh':
                return `${year}-${month}-${day} ${zeroHour}`;
            case 'YYYY-M-D':
                return `${year}-${month}-${day}`;
            case 'YYYY/MM/DD hh:mm:ss':
                return `${year}/${zeroMonth}/${zeroDay} ${zeroHour}:${zeroMin}:${zeroSen}`;
            case 'YYYY/M/D hh:mm:ss':
                return `${year}/${month}/${day} ${zeroHour}:${zeroMin}:${zeroSen}`;
            case 'YYYY/MM/DD':
                return `${year}/${zeroMonth}/${zeroDay}`;
            case 'YYYY/M/D':
                return `${year}/${month}/${day}`;
            case 'DD':
                return `${day}`;
            case 'hh:mm':
                return `${zeroHour}:${zeroMin}`;
            case '年月日时分秒':
                return `${year}年${month}月${day}日 ${hour}时${min}分${sen}秒`;
            case '年月日时分':
                return `${year}年${month}月${day}日 ${hour}时${min}分`;
            case '年月日时':
                return `${year}年${month}月${day}日 ${hour}时`;
            case '年月日':
                return `${year}年${month}月${day}日`;
            case '年月':
                return `${year}年${month}月`;
            case '月日':
                return `${month}月${day}日`;
            case '年':
                return `${year}年`;
            case '月':
                return `${month}月`;
            case '日':
                return `${day}日`;
        }
    },
    // 传入毫秒数计算几天前，几小时前，几分钟前，几秒前
    distance:(hisTime:number) => {
        try{
            if(hisTime.toString().length==10){
                hisTime *= 1000;
            }
        }catch(e){
            return '';
        }
        let now = new Date().getTime(),
        diffValue = now - hisTime,
        result='',		
        minute = 1000 * 60,
        hour = minute * 60,
        day = hour * 24,
        month = day * 30,
        year = month * 12,		
        _year = diffValue/year,
        _month =diffValue/month,
        _week =diffValue/(7*day),
        _day =diffValue/day,
        _hour =diffValue/hour,
        _min =diffValue/minute;
        if(_year>=1) result=parseInt(_year.toString()) + "年前";
        else if(_month>=1) result=parseInt(_month.toString()) + "个月前";
        else if(_week>=1) result=parseInt(_week.toString()) + "周前";
        else if(_day>=1) result=parseInt(_day.toString()) +"天前";
        else if(_hour>=1) result=parseInt(_hour.toString()) +"小时前";
        else if(_min>=1) result=parseInt(_min.toString()) +"分钟前";
        else result="刚刚";
        return result;
    },
    // 获取星期几
    getWeek(time:number) {
        return "日一二三四五六".charAt(new Date(time).getDay()); 
    }	
}